package api

import (
	"github.com/emicklei/go-restful/v3"
	"github.com/infraboard/mcube/http/request"
	"github.com/infraboard/mcube/http/response"
	"kubernetes-manage/apps/cluster"
)

func (h *handler) CreateCluster(r *restful.Request, w *restful.Response) {

	req := cluster.NewCreateClusterRequest()
	if err := request.GetDataFromRequest(r.Request, req); err != nil {
		response.Failed(w, err)
		h.log.Errorf("error: %s", err)
		return
	}

	// 从paramet中传递参数，并赋值到req对象中
	cluster_name := r.QueryParameter("cluster_name")
	req.ClusterName = cluster_name
	kube_config := r.QueryParameter("kube_config")
	req.KubeConfig = kube_config

	set, err := h.service.CreateCluster(r.Request.Context(), req)
	if err != nil {
		response.Failed(w, err)
		h.log.Errorf("error: %s", err)
		return
	}

	response.Success(w, set)

}

func (h *handler) QueryCluster(r *restful.Request, w *restful.Response) {
	req := cluster.NewCreateClusterRequest()

	// 获取body 表单数据
	if err := request.GetDataFromRequest(r.Request, req); err != nil {
		response.Failed(w, err)
		h.log.Debugf("error: %s", err)
		return
	}

	set, err := h.service.QueryCluster(r.Request.Context(), req)
	if err != nil {
		response.Failed(w, err)
		return
	}
	response.Success(w, set)

}
